package main

import (
	"gitee.com/jinmingzhi/cdce-service/internal/handle"
	"gitee.com/jinmingzhi/cdce-service/internal/repository"
	"gitee.com/jinmingzhi/cdce-service/internal/service"
	"gitee.com/jinmingzhi/cdce-service/pkg/auth"
	"gitee.com/jinmingzhi/cdce-service/pkg/middleware"
	"gitee.com/jinmingzhi/kit/app"
	"gitee.com/jinmingzhi/kit/mlog"
	"gitee.com/jinmingzhi/kit/rest"
	middleware2 "gitee.com/jinmingzhi/kit/rest/middleware"
	"gitee.com/jinmingzhi/kit/rpc"
)

var (
	log = mlog.Logger("cmd")
)

func main() {
	log.Info("init repository")
	repository.InitGlobal()

	log.Info("init service")
	service.InitGlobal()

	log.Info("init handler")
	handle.InitGlobal()

	log.Info("init rpc")
	// grpc 中间件
	rpcServer := rpc.NewServerWithConfig(middleware.Auth())
	handle.RegisterGRPC(rpcServer.Server)
	rpcServer.MustListenAndServe()

	log.Info("init rest")

	// 插入中间件，中间会有下往上执行
	restServer := rest.NewServerWithConfig(
		//middleware.Auth,
		middleware2.RegisterCORSHandler(rest.MustLoadConfig().CORSMaxAge),
		)
	handle.MustRegisterREST(restServer.ServeMux, rpcServer.Address)
	handle.ServeSwaggerUI(restServer.HttpServeMux)
	restServer.HttpServeMux.HandleFunc("/login", auth.Signin)
	restServer.HttpServeMux.HandleFunc("/refresh", auth.Refresh)
	restServer.ListenAndServed()
	app.Exit()
}
